package com.aiflow.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;


public class test {

	public static void main(String[] args) {
		String line = "";
		PreparedStatement pst = null;
		Connection conn = null;
		List<String> list = new ArrayList<String>();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/手机号段归属地数据库.txt")));
			while((line = br.readLine()) != null){
				list.add(line);
			}
			String driver = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://192.168.0.198:3306/80core";
			String user = "myuser";
			String password = "123456";
			Class.forName(driver);
			conn = (Connection) DriverManager.getConnection(url, user, password);
			conn.setAutoCommit(false);
			String sql = "insert into num_t(num,region,type,code) values(?,?,?,?)";
			pst = (PreparedStatement) conn.prepareStatement(sql);
			System.out.println(list.size());
			for (int i = 0; i < list.size(); i++) {
				    String str = list.get(i);
				    pst.clearParameters();
					pst.setString(1, str.split("\\s{1,}")[0]);  
					pst.setString(2, str.split("\\s{1,}")[1]);
					pst.setString(3, str.split("\\s{1,}")[2]);
					pst.setString(4, str.split("\\s{1,}")[3]);
					pst.execute();
					if(i % 8000 == 0){
						conn.commit();
					}
			}
			conn.commit();
			// 语句执行完毕，提交本事务  
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				pst.close();  
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
